// 导入db模块
const db = require('../config/db');

// 定义用户类
class User {
    // 注册
    register(user_id, user_acc, user_pwd, user_create_time) {
        return new Promise((resolve, reject) => {
            // 查询sql语句
            let sql = `select user_acc from db_user where user_acc='${user_acc}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        // 执行成功
                        if (rows.length > 0) {
                            // 表示账号已经存在
                            resolve({
                                code: 200,
                                msg: '该账号已经存在。',
                                user_acc
                            })
                        }
                        else {
                            // 添加账号信息
                            // 插入数据sql语句
                            let sql2 = `insert into db_user(user_id, user_acc, user_pwd,user_create_time,user_sex) values('${user_id}','${user_acc}','${user_pwd}', '${user_create_time}','未知')`;
                            // 返回添加数据的promise
                            return db.query(sql2)

                        }
                    },
                    () => {
                        // 执行失败
                        reject({
                            code: -1,
                            msg: '查询用户失败。'
                        })
                    }
                )
                .then(
                    (data) => {
                        resolve({
                            code: 200,
                            msg: '注册账号成功。',
                            user_acc,
                            user_create_time
                        })
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '注册账号失败。',
                            err
                        })
                    }
                )
        })
    }

    // 登录
    login(user_acc) {
        return new Promise((resolve, reject) => {
            // sql语句 (查询账号和密码和用户id  user_acc, user_pwd, user_id)
            let sql = `select user_acc, user_pwd, user_id from db_user where user_acc = '${user_acc}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        // 处理用户信息
                        if (rows.length == 0) {
                            resolve({
                                code: 404,
                                msg: '该账号不存在。'
                            })
                        }
                        else {
                            // [{user_acc, user_pwd , user_id}]
                            resolve(rows)
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询用户信息失败。',
                            err
                        })
                    }
                )
        })
    }

    // 找回密码的
    findpwd(user_acc, new_user_pwd) {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select user_acc from db_user where user_acc = '${user_acc}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 修改数据表的sql语句
                            let sql2 = `update db_user set user_pwd= '${new_user_pwd}' where  user_acc = '${user_acc}'`;
                            db.query(sql2).then(
                                content => {
                                    resolve(content);
                                },
                                err => {
                                    reject(err)
                                }
                            )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该账户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询账号失败。'
                        })
                    }
                )
        })
    }

    // 获取用户信息
    info(user_id) {
        return new Promise((resolve, reject) => {
            // console.log("y.3",user_id);
            // 查询账号
            let sql = `select user_name , user_acc, user_src, user_age, user_address, user_create_time, user_tel, user_sex from db_user where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        // console.log('y.2',rows);
                        if (rows.length == 1) {
                            resolve({
                                code: 200,
                                msg: '获取用户信息成功。',
                                data: rows
                            })
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该账户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询账号失败。'
                        })
                    }
                )
        })
    }

     // 获取用户信息(给评论回复使用的)
     user_info(user_id) {
        return new Promise((resolve, reject) => {
            // console.log("y.3",user_id);
            // 查询账号
            let sql = `select user_name , user_acc, user_src, user_age, user_address, user_create_time, user_tel, user_sex from db_user where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        // console.log('y.2',rows);
                        if (rows.length == 1) {
                            resolve({
                                code: 200,
                                msg: '获取用户信息成功。',
                                data: rows
                            })
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该账户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询账号失败。'
                        })
                    }
                )
        })
    }

    // 删除账号
    remove(user_acc, user_id) {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select user_acc from db_user where user_acc = '${user_acc}' and user_id='${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 删除数据表的sql语句
                            let sql2 = `delete from db_user where user_id = '${user_id}'`
                            db.query(sql2).then(
                                (result) => {
                                    if (result.affectedRows === 1) {
                                        resolve({
                                            code: 200,
                                            msg: '删除用户账号成功。',
                                        })
                                    }
                                    else {
                                        resolve({
                                            code: -1,
                                            msg: '删除用户账号失败。',
                                        })
                                    }
                                },
                                err => {
                                    reject(err)
                                }
                            )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该账户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询账号失败。'
                        })
                    }
                )
        })
    }

    // 修改用户数据
    modify(user_id, user_sex, user_age, user_address, user_tel,user_name) {
        return new Promise((resolve, reject) => {
            let sql = `select user_id from db_user where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 修改用户资料
                            let sql2 = `update db_user set user_sex= '${user_sex}', user_age= '${user_age}', user_address= '${user_address}', user_tel= '${user_tel}', user_name='${user_name}' where  user_id= '${user_id}'`;

                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // 判断是否修改成功
                                        if (result.affectedRows == 1) {
                                            resolve({
                                                code: 200,
                                                msg: '修改用户资料成功。'
                                            })
                                        }
                                        else {
                                            resolve({
                                                code: -1,
                                                msg: '修改用户资料失败。'
                                            })
                                        }

                                    },
                                    (err) => {
                                        reject({
                                            code: -1,
                                            msg: '服务端发生异常。222',
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该用户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '服务端发生异常。111',
                            err
                        })
                    }
                )
        })
    }

    // 设置用户头像
    modify_user_src(user_id, user_src) {
        return new Promise((resolve, reject) => {
            let sql = `select user_id from db_user where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 修改用户资料
                            let sql2 = `update db_user set user_src= '${user_src}' where  user_id= '${user_id}'`;

                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // 判断是否修改成功
                                        if (result.affectedRows == 1) {
                                            resolve({
                                                code: 200,
                                                msg: '设置用户头像成功。',
                                                user_src
                                            })
                                        }
                                        else {
                                            resolve({
                                                code: -1,
                                                msg: '设置用户头像失败。'
                                            })
                                        }

                                    },
                                    (err) => {
                                        reject({
                                            code: -1,
                                            msg: '服务端发生异常。222',
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该用户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '服务端发生异常。111',
                            err
                        })
                    }
                )
        })
    }

    // 获取性别对应的用户数据
    total_user_sex(user_sex) {
        return new Promise((resolve, reject) => {
            // 统计db_message这个表格的内容数量
            let sql = `select count(*) as total from db_user where user_sex = '${user_sex}'`
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: `统计${user_sex}用户数量成功。`,
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '统计数据失败。'
                        })
                    }
                )
        })
    }

    // 获取所有用户数量
    total() {
        return new Promise((resolve, reject) => {
            // 统计db_message这个表格的内容数量
            let sql = `select count(*) as total from db_user`
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: `统计用户数量成功。`,
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '统计数据失败。'
                        })
                    }
                )
        })
    }

    // 修改用户名
    modify_name(user_id, user_name) {
        return new Promise((resolve, reject) => {
            let sql = `select user_id from db_user where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 修改用户资料
                            let sql2 = `update db_user set user_name= '${user_name}' where  user_id= '${user_id}'`;

                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // 判断是否修改成功
                                        if (result.affectedRows == 1) {
                                            resolve({
                                                code: 200,
                                                msg: '修改用户名成功。'
                                            })
                                        }
                                        else {
                                            resolve({
                                                code: -1,
                                                msg: '修改用户名失败。'
                                            })
                                        }

                                    },
                                    (err) => {
                                        reject({
                                            code: -1,
                                            msg: '服务端发生异常。222',
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该用户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '服务端发生异常。111',
                            err
                        })
                    }
                )
        })
    }

}

module.exports = new User();